<?php namespace App\Client\CabinetCell\Criterias;

use Input;
use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
//基础条件
use App\Core\Repository\Criterias\NotDeleteCriteria;
use App\Core\Repository\Criterias\FixedCriteria;
//Cabinet
use App\Admin\Cabinet\Cabinet\Repository as CabinetRepository;

class MatchCabinet extends Criteria{

    public function apply(Repository $repository){
        parent::apply($repository);
        $cabinetId = Input::get('cabinet_id');
        if(!$cabinetId || !is_numeric($cabinetId)){
            return $this->query->whereRaw('1=0');
        }
        $this->cabinetId = $cabinetId;
        $rep = new CabinetRepository();
        $rep->addCriteria(new NotDeleteCriteria());
        $rep->addCriteria(new FixedCriteria('id', $cabinetId));
        $cabinet = $rep->first();
        if($cabinet){
            $field = $this->formatField($this->model, 'cabinet_id');
            $this->query->where($field, '=', $cabinet->id);
            return ;
        }
        $this->query->whereRaw('1=0');
    }
}